package com.knife.interfaces.concurrent;


import com.knife.common.utils.KnifeSpringUtil;
import com.knife.interfaces.entity.KnifeInterfaceLogRecord;
import com.knife.interfaces.extension.config.AmapKeyProperties;
import com.knife.interfaces.extension.producer.KnifeInterfaceLogMessageProducer;
import com.knife.interfaces.mapper.KnifeInterfaceLogRecordMapper;

/**
 * 1 记录接口请求日志的线程
 *
 * @author: tianhao.luo@hand-china.com 2021/6/30  上午11:02
 */
public class KnifeInterfaceRecordThread implements Runnable {
    private final static KnifeInterfaceLogRecordMapper KNIFE_INTERFACE_LOG_RECORD_MAPPER;
    private final static KnifeInterfaceLogMessageProducer DEFAULT_MQ_PRODUCER;
    private final static AmapKeyProperties AMAP_KEY_PROPERTIES;

    static {
        // 1.加载mapper
        KNIFE_INTERFACE_LOG_RECORD_MAPPER = KnifeSpringUtil.getBean(KnifeInterfaceLogRecordMapper.class);
        DEFAULT_MQ_PRODUCER = KnifeSpringUtil.getBean(KnifeInterfaceLogMessageProducer.class);
        AMAP_KEY_PROPERTIES = KnifeSpringUtil.getBean(AmapKeyProperties.class);
    }

    private final KnifeInterfaceLogRecord knifeInterfaceLogRecord;

    public KnifeInterfaceRecordThread(KnifeInterfaceLogRecord knifeInterfaceLogRecord) {
        this.knifeInterfaceLogRecord = knifeInterfaceLogRecord;
    }

    @Override
    public void run() {
        // 保存数据,因为该日志表没有表主键,所以不需要害怕插入冲突的问题
        KNIFE_INTERFACE_LOG_RECORD_MAPPER.saveAndFlush(knifeInterfaceLogRecord);
        if (AMAP_KEY_PROPERTIES.getEnableFlag()){
            DEFAULT_MQ_PRODUCER.pushInterfaceLogAfterEndurance(knifeInterfaceLogRecord);
        }
    }
}
